package com.own.business.login.core.core;

import com.own.component.common.hook.business.LoginOperationHook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * LoginOperationSpringHook
 *
 * @author chenxueli
 * @date 2023-04-18 13:37:00
 */
@Slf4j
@Component
@SuppressWarnings("all")
public class LoginOperationSpringHook implements ApplicationContextAware, ApplicationListener<ApplicationEvent> {

    private static final List<LoginOperationHook> LOGIN_OPERATION_HOOK_LIST = new ArrayList<>();
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationReadyEvent) {
            applicationContext.getBeansOfType(LoginOperationHook.class).forEach((key, hook) -> {
                LOGIN_OPERATION_HOOK_LIST.add(hook);
                log.debug("登录动作钩子方法===>【{}】完成注册", key);
            });
        }
    }

}
